{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[TOC]\n",
    "\n",
    "<!-- Interfaces -->\n",
    "# 第十章 接口\n",
    "\n",
    "接口和抽象类提供了一种将接口与实现分离的更加结构化的方法。\n",
    "\n",
    "这种机制在编程语言中不常见，例如 C++ 只对这种概念有间接的支持。而在 Java 中存在这些关键字，说明这些思想很重要，Java 为它们提供了直接支持。\n",
    "\n",
    "首先，我们将学习抽象类，一种介于普通类和接口之间的折中手段。尽管你的第一想法是创建接口，但是对于构建具有属性和未实现方法的类来说，抽象类也是重要且必要的工具。你不可能总是使用纯粹的接口。\n",
    "\n",
    "\n",
    "<!-- Abstract Classes and Methods -->\n",
    "## 抽象类和方法\n",
    "\n",
    "在上一章的乐器例子中，基类 **Instrument** 中的方法往往是“哑”方法。如果调用了这些方法，就会出现一些错误。这是因为接口的目的是为它的派生类创建一个通用接口。\n",
    "\n",
    "在那些例子中，创建这个通用接口的唯一理由是，不同的子类可以用不同的方式表示此接口。通用接口建立了一个基本形式，以此表达所有派生类的共同部分。另一种说法把 **Instrument** 称为抽象基类，或简称抽象类。\n",
    "\n",
    "对于像 **Instrument** 那样的抽象类来说，它的对象几乎总是没有意义的。创建一个抽象类是为了通过通用接口操纵一系列类。因此，**Instrument** 只是表示接口，不是具体实现，所以创建一个 **Instrument** 的对象毫无意义，我们可能希望阻止用户这么做。通过让 **Instrument** 所有的方法产生错误，就可以达到这个目的，但是这么做会延迟到运行时才能得知错误信息，并且需要用户进行可靠、详尽的测试。最好能在编译时捕捉问题。\n",
    "\n",
    "Java 提供了一个叫做*抽象方法*的机制，这个方法是不完整的：它只有声明没有方法体。下面是抽象方法的声明语法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "abstract void f();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "包含抽象方法的类叫做*抽象类*。如果一个类包含一个或多个抽象方法，那么类本身也必须限定为抽象的，否则，编译器会报错。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interface/Basic.java\n",
    "abstract class Basic {\n",
    "    abstract void unimplemented();\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果一个抽象类是不完整的，当试图创建这个类的对象时，Java 会怎么做呢？它不会创建抽象类的对象，所以我们只会得到编译器的错误信息。这样保证了抽象类的纯粹性，我们不用担心误用它。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/AttemptToUseBasic.java\n",
    "// {WillNotCompile}\n",
    "public class AttemptToUseBasic {\n",
    "    Basic b = new Basic();\n",
    "    // error: Basic is abstract; cannot be instantiated\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果创建一个继承抽象类的新类并为之创建对象，那么就必须为基类的所有抽象方法提供方法定义。如果不这么做（可以选择不做），新类仍然是一个抽象类，编译器会强制我们为新类加上 **abstract** 关键字。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/Basic2.java\n",
    "abstract class Basic2 extends Basic {\n",
    "    int f() {\n",
    "        return 111;\n",
    "    }\n",
    "    \n",
    "    abstract void g() {\n",
    "        // unimplemented() still not implemented\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以将一个不包含任何抽象方法的类指明为 **abstract**，在类中的抽象方法没啥意义但想阻止创建类的对象时，这么做就很有用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/AbstractWithoutAbstracts.java\n",
    "abstract class Basic3 {\n",
    "    int f() {\n",
    "        return 111;\n",
    "    }\n",
    "    \n",
    "    // No abstract methods\n",
    "}\n",
    "\n",
    "public class AbstractWithoutAbstracts {\n",
    "    // Basic b3 = new Basic3();\n",
    "    // error: Basic 3 is abstract; cannot be instantiated\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "为了创建可初始化的类，就要继承抽象类，并提供所有抽象方法的定义："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/Instantiable.java\n",
    "abstract class Uninstantiable {\n",
    "    abstract void f();\n",
    "    abstract int g();\n",
    "}\n",
    "\n",
    "public class Instantiable extends Uninstantiable {\n",
    "    @Override\n",
    "    void f() {\n",
    "        System.out.println(\"f()\");\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    int g() {\n",
    "        return 22;\n",
    "    }\n",
    "    \n",
    "    public static void main(String[] args) {\n",
    "        Uninstantiable ui = new Instantiable();\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "留意 `@Override` 的使用。没有这个注解的话，如果你没有定义相同的方法名或签名，抽象机制会认为你没有实现抽象方法从而产生编译时错误。因此，你可能认为这里的 `@Override` 是多余的。但是，`@Override` 还提示了这个方法被覆写——我认为这是有用的，所以我会使用 `@Override`，即使在没有这个注解，编译器告诉我错误的时候。 \n",
    "\n",
    "记住，事实上的访问权限是“friendly”。你很快会看到接口自动将其方法指明为 **public**。事实上，接口只允许 **public** 方法，如果不加访问修饰符的话，接口的方法不是 **friendly** 而是 **public**。然而，抽象类允许每件事："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/AbstractAccess.java\n",
    "abstract class AbstractAccess {\n",
    "    private void m1() {}\n",
    "    \n",
    "    // private abstract void m1a(); // illegal\n",
    "    \n",
    "    protected void m2() {}\n",
    "    \n",
    "    protected abstract void m2a();\n",
    "    \n",
    "    void m3() {}\n",
    "    \n",
    "    abstract void m3a();\n",
    "    \n",
    "    public void m4() {}\n",
    "    \n",
    "    public abstract void m4a();\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**private abstract** 被禁止了是有意义的，因为你不可能在 **AbstractAccess** 的任何子类中合法地定义它。\n",
    "\n",
    "上一章的 **Instrument** 类可以很轻易地转换为一个抽象类。只需要部分方法是 **abstract** 即可。将一个类指明为 **abstract** 并不强制类中的所有方法必须都是抽象方法。如下图所示：\n",
    "\n",
    "![类图](../images/1562653648586.png)\n",
    "\n",
    "下面是修改成使用抽象类和抽象方法的管弦乐器的例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/music4/Music4.java\n",
    "// Abstract classes and methods\n",
    "// {java interfaces.music4.Music4}\n",
    "package interfaces.music4;\n",
    "import polymorphism.music.Note;\n",
    "\n",
    "abstract class Instrument {\n",
    "    private int i; // Storage allocated for each\n",
    "    \n",
    "    public abstract void play(Note n);\n",
    "    \n",
    "    public String what() {\n",
    "        return \"Instrument\";\n",
    "    }\n",
    "    \n",
    "    public abstract void adjust();\n",
    "}\n",
    "\n",
    "class Wind extends Instrument {\n",
    "    @Override\n",
    "    public void play(Note n) {\n",
    "        System.out.println(\"Wind.play() \" + n);\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public String what() {\n",
    "        return \"Wind\";\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public void adjust() {\n",
    "        System.out.println(\"Adjusting Wind\");\n",
    "    }\n",
    "}\n",
    "\n",
    "class Percussion extends Instrument {\n",
    "    @Override\n",
    "    public void play(Note n) {\n",
    "        System.out.println(\"Percussion.play() \" + n);\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public String what() {\n",
    "        return \"Percussion\";\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public void adjust() {\n",
    "        System.out.println(\"Adjusting Percussion\");\n",
    "    }\n",
    "}\n",
    "\n",
    "class Stringed extends Instrument {\n",
    "    @Override\n",
    "    public void play(Note n) {\n",
    "        System.out.println(\"Stringed.play() \" + n);\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public String what() {\n",
    "        return \"Stringed\";\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public void adjust() {\n",
    "        System.out.println(\"Adjusting Stringed\");\n",
    "    }\n",
    "}\n",
    "\n",
    "class Brass extends Wind {\n",
    "    @Override\n",
    "    public void play(Note n) {\n",
    "        System.out.println(\"Brass.play() \" + n);\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public void adjust() {\n",
    "        System.out.println(\"Adjusting Brass\");\n",
    "    }\n",
    "}\n",
    "\n",
    "class Woodwind extends Wind {\n",
    "    @Override\n",
    "    public void play(Note n) {\n",
    "        System.out.println(\"Woodwind.play() \" + n);\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public String what() {\n",
    "        return \"Woodwind\";\n",
    "    }\n",
    "}\n",
    "\n",
    "public class Music4 {\n",
    "    // Doesn't care about type, so new types\n",
    "    // added to system still work right:\n",
    "    static void tune(Instrument i) {\n",
    "        // ...\n",
    "        i.play(Note.MIDDLE_C);\n",
    "    }\n",
    "    \n",
    "    static void tuneAll(Instrument[] e) {\n",
    "        for (Instrument i: e) {\n",
    "            tune(i);\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    public static void main(String[] args) {\n",
    "        // Upcasting during addition to the array:\n",
    "        Instrument[] orchestra = {\n",
    "            new Wind(),\n",
    "            new Percussion(),\n",
    "            new Stringed(),\n",
    "            new Brass(),\n",
    "            new Woodwind()\n",
    "        };\n",
    "        tuneAll(orchestra);\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Wind.play() MIDDLE_C\n",
    "Percussion.play() MIDDLE_C\n",
    "Stringed.play() MIDDLE_C\n",
    "Brass.play() MIDDLE_C\n",
    "Woodwind.play() MIDDLE_C"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "除了 **Instrument**，基本没区别。\n",
    "\n",
    "创建抽象类和抽象方法是有帮助的，因为它们使得类的抽象性很明确，并能告知用户和编译器使用意图。抽象类同时也是一种有用的重构工具，使用它们使得我们很容易地将沿着继承层级结构上移公共方法。\n",
    "\n",
    "<!-- Interfaces -->\n",
    "\n",
    "## 接口创建\n",
    "\n",
    "使用 **interface** 关键字创建接口。在本书中，interface 和 class 一样随处常见，除非特指关键字 **interface**，其他情况下都采用正常字体书写 interface。\n",
    "\n",
    "描述 Java 8 之前的接口更加容易，因为它们只允许抽象方法。像下面这样："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/PureInterface.java\n",
    "// Interface only looked like this before Java 8\n",
    "public interface PureInterface {\n",
    "    int m1(); \n",
    "    void m2();\n",
    "    double m3();\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们甚至不用为方法加上 **abstract** 关键字，因为方法在接口中。Java 知道这些方法不能有方法体（仍然可以为方法加上 **abstract** 关键字，但是看起来像是不明白接口，徒增难堪罢了）。\n",
    "\n",
    "因此，在 Java 8之前我们可以这么说：**interface** 关键字产生一个完全抽象的类，没有提供任何实现。我们只能描述类应该像什么，做什么，但不能描述怎么做，即只能决定方法名、参数列表和返回类型，但是无法确定方法体。接口只提供形式，通常来说没有实现，尽管在某些受限制的情况下可以有实现。\n",
    "\n",
    "一个接口表示：所有实现了该接口的类看起来都像这样。因此，任何使用某特定接口的代码都知道可以调用该接口的哪些方法，而且仅需知道这些。所以，接口被用来建立类之间的协议。（一些面向对象编程语言中，使用 protocol 关键字完成相同的功能。）\n",
    "\n",
    "Java 8 中接口稍微有些变化，因为 Java 8 允许接口包含默认方法和静态方法——基于某些重要原因，看到后面你会理解。接口的基本概念仍然没变，介于类型之上、实现之下。接口与抽象类最明显的区别可能就是使用上的惯用方式。接口的典型使用是代表一个类的类型或一个形容词，如 Runnable 或 Serializable，而抽象类通常是类层次结构的一部分或一件事物的类型，如 String 或 ActionHero。\n",
    "\n",
    "使用关键字 **interface** 而不是 **class** 来创建接口。和类一样，需要在关键字 **interface** 前加上 **public** 关键字（但只是在接口名与文件名相同的情况下），否则接口只有包访问权限，只能在接口相同的包下才能使用它。\n",
    "\n",
    "接口同样可以包含属性，这些属性被隐式指明为 **static** 和 **final**。\n",
    "\n",
    "使用 **implements** 关键字使一个类遵循某个特定接口（或一组接口），它表示：接口只是外形，现在我要说明它是如何工作的。除此之外，它看起来像继承。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/ImplementingAnInterface.java\n",
    "interface Concept { // Package access\n",
    "    void idea1();\n",
    "    void idea2();\n",
    "}\n",
    "\n",
    "class Implementation implements Concept {\n",
    "    @Override\n",
    "    public void idea1() {\n",
    "        System.out.println(\"idea1\");\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public void idea2() {\n",
    "        System.out.println(\"idea2\");\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你可以选择显式地声明接口中的方法为 **public**，但是即使你不这么做，它们也是 **public** 的。所以当实现一个接口时，来自接口中的方法必须被定义为 **public**。否则，它们只有包访问权限，这样在继承时，它们的可访问权限就被降低了，这是 Java 编译器所不允许的。\n",
    "\n",
    "### 默认方法\n",
    "\n",
    "Java 8 为关键字 **default** 增加了一个新的用途（之前只用于 **switch** 语句和注解中）。当在接口中使用它时，任何实现接口却没有定义方法的时候可以使用 **default** 创建的方法体。默认方法比抽象类中的方法受到更多的限制，但是非常有用，我们将在“流式编程”一章中看到。现在让我们看下如何使用："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/AnInterface.java\n",
    "interface AnInterface {\n",
    "    void firstMethod();\n",
    "    void secondMethod();\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以像这样实现接口："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/AnImplementation.java\n",
    "public class AnImplementation implements AnInterface {\n",
    "    public void firstMethod() {\n",
    "        System.out.println(\"firstMethod\");\n",
    "    }\n",
    "    \n",
    "    public void secondMethod() {\n",
    "        System.out.println(\"secondMethod\");\n",
    "    }\n",
    "    \n",
    "    public static void main(String[] args) {\n",
    "        AnInterface i = new AnImplementation();\n",
    "        i.firstMethod();\n",
    "        i.secondMethod();\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "firstMethod\n",
    "secondMethod"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果我们在 **AnInterface** 中增加一个新方法 `newMethod()`，而在 **AnImplementation** 中没有实现它，编译器就会报错："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "AnImplementation.java:3:error: AnImplementation is not abstract and does not override abstract method newMethod() in AnInterface\n",
    "public class AnImplementation implements AnInterface {\n",
    "^\n",
    "1 error"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果我们使用关键字 **default** 为 `newMethod()` 方法提供默认的实现，那么所有与接口有关的代码能正常工作，不受影响，而且这些代码还可以调用新的方法 `newMethod()`："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/InterfaceWithDefault.java\n",
    "interface InterfaceWithDefault {\n",
    "    void firstMethod();\n",
    "    void secondMethod();\n",
    "    \n",
    "    default void newMethod() {\n",
    "        System.out.println(\"newMethod\");\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "关键字 **default** 允许在接口中提供方法实现——在 Java 8 之前被禁止。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/Implementation2.java\n",
    "public class Implementation2 implements InterfaceWithDefault {\n",
    "    @Override\n",
    "    public void firstMethod() {\n",
    "        System.out.println(\"firstMethod\");\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public void secondMethod() {\n",
    "        System.out.println(\"secondMethod\")\n",
    "    }\n",
    "    \n",
    "    public static void main(String[] args) {\n",
    "        InterfaceWithDefault i = new Implementation2();\n",
    "        i.firstMethod();\n",
    "        i.secondMethod();\n",
    "        i.newMethod();\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "firstMethod\n",
    "secondMethod\n",
    "newMethod"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "尽管 **Implementation2** 中未定义 `newMethod()`，但是可以使用 `newMethod()` 了。 \n",
    "\n",
    "增加默认方法的极具说服力的理由是它允许在不破坏已使用接口的代码的情况下，在接口中增加新的方法。默认方法有时也被称为*守卫方法*或*虚拟扩展方法*。\n",
    "\n",
    "### 多继承\n",
    "\n",
    "多继承意味着一个类可能从多个父类型中继承特征和特性。\n",
    "\n",
    "Java 在设计之初，C++ 的多继承机制饱受诟病。Java 过去是一种严格要求单继承的语言：只能继承自一个类（或抽象类），但可以实现任意多个接口。在 Java 8 之前，接口没有包袱——它只是方法外貌的描述。\n",
    "\n",
    "多年后的现在，Java 通过默认方法具有了某种多继承的特性。结合带有默认方法的接口意味着结合了多个基类中的行为。因为接口中仍然不允许存在属性（只有静态属性，不适用），所以属性仍然只会来自单个基类或抽象类，也就是说，不会存在状态的多继承。正如下面这样："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/MultipleInheritance.java\n",
    "import java.util.*;\n",
    "\n",
    "interface One {\n",
    "    default void first() {\n",
    "        System.out.println(\"first\");\n",
    "    }\n",
    "}\n",
    "\n",
    "interface Two {\n",
    "    default void second() {\n",
    "        System.out.println(\"second\");\n",
    "    }\n",
    "}\n",
    "\n",
    "interface Three {\n",
    "    default void third() {\n",
    "        System.out.println(\"third\");\n",
    "    }\n",
    "}\n",
    "\n",
    "class MI implements One, Two, Three {}\n",
    "\n",
    "public class MultipleInheritance {\n",
    "    public static void main(String[] args) {\n",
    "        MI mi = new MI();\n",
    "        mi.first();\n",
    "        mi.second();\n",
    "        mi.third();\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "first\n",
    "second\n",
    "third"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在我们做些在 Java 8 之前不可能完成的事：结合多个源的实现。只要基类方法中的方法名和参数列表不同，就能工作得很好，否则会得到编译器错误："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interface/MICollision.java\n",
    "import java.util.*;\n",
    "\n",
    "interface Bob1 {\n",
    "    default void bob() {\n",
    "        System.out.println(\"Bob1::bob\");\n",
    "    }\n",
    "}\n",
    "\n",
    "interface Bob2 {\n",
    "    default void bob() {\n",
    "        System.out.println(\"Bob2::bob\");\n",
    "    }\n",
    "}\n",
    "\n",
    "// class Bob implements Bob1, Bob2 {}\n",
    "/* Produces:\n",
    "error: class Bob inherits unrelated defaults\n",
    "for bob() from types Bob1 and Bob2\n",
    "class Bob implements Bob1, Bob2 {}\n",
    "^\n",
    "1 error\n",
    "*/\n",
    "\n",
    "interface Sam1 {\n",
    "    default void sam() {\n",
    "        System.out.println(\"Sam1::sam\");\n",
    "    }\n",
    "}\n",
    "\n",
    "interface Sam2 {\n",
    "    default void sam(int i) {\n",
    "        System.out.println(i * 2);\n",
    "    }\n",
    "}\n",
    "\n",
    "// This works because the argument lists are distinct:\n",
    "class Sam implements Sam1, Sam2 {}\n",
    "\n",
    "interface Max1 {\n",
    "    default void max() {\n",
    "        System.out.println(\"Max1::max\");\n",
    "    }\n",
    "}\n",
    "\n",
    "interface Max2 {\n",
    "    default int max() {\n",
    "        return 47;\n",
    "    }\n",
    "}\n",
    "\n",
    "// class Max implements Max1, Max2 {}\n",
    "/* Produces:\n",
    "error: types Max2 and Max1 are imcompatible;\n",
    "both define max(), but with unrelated return types\n",
    "class Max implements Max1, Max2 {}\n",
    "^\n",
    "1 error\n",
    "*/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Sam** 类中的两个 `sam()` 方法有相同的方法名但是签名不同——方法签名包括方法名和参数类型，编译器也是用它来区分方法。但是从 **Max** 类可看出，返回类型不是方法签名的一部分，因此不能用来区分方法。为了解决这个问题，需要覆写冲突的方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/Jim.java\n",
    "import java.util.*;\n",
    "\n",
    "interface Jim1 {\n",
    "    default void jim() {\n",
    "        System.out.println(\"Jim1::jim\");\n",
    "    }\n",
    "}\n",
    "\n",
    "interface Jim2 {\n",
    "    default void jim() {\n",
    "        System.out.println(\"Jim2::jim\");\n",
    "    }\n",
    "}\n",
    "\n",
    "public class Jim implements Jim1, Jim2 {\n",
    "    @Override\n",
    "    public void jim() {\n",
    "        Jim2.super.jim();\n",
    "    }\n",
    "    \n",
    "    public static void main(String[] args) {\n",
    "        new Jim().jim();\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Jim2::jim"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当然，你可以重定义 `jim()` 方法，但是也能像上例中那样使用 **super** 关键字选择基类实现中的一种。\n",
    "\n",
    "### 接口中的静态方法\n",
    "\n",
    "Java 8 允许在接口中添加静态方法。这么做能恰当地把工具功能置于接口中，从而操作接口，或者成为通用的工具："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// onjava/Operations.java\n",
    "package onjava;\n",
    "import java.util.*;\n",
    "\n",
    "public interface Operations {\n",
    "    void execute();\n",
    "    \n",
    "    static void runOps(Operations... ops) {\n",
    "        for (Operations op: ops) {\n",
    "            op.execute();\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    static void show(String msg) {\n",
    "        System.out.println(msg);\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这是模版方法设计模式的一个版本（在“设计模式”一章中详细描述），`runOps()` 是一个模版方法。`runOps()` 使用可变参数列表，因而我们可以传入任意多的 **Operation** 参数并按顺序运行它们："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interface/Machine.java\n",
    "import java.util.*;\n",
    "import onjava.Operations;\n",
    "\n",
    "class Bing implements Operations {\n",
    "    @Override\n",
    "    public void execute() {\n",
    "        Operations.show(\"Bing\");\n",
    "    }\n",
    "}\n",
    "\n",
    "class Crack implements Operations {\n",
    "    @Override\n",
    "    public void execute() {\n",
    "        Operations.show(\"Crack\");\n",
    "    }\n",
    "}\n",
    "\n",
    "class Twist implements Operations {\n",
    "    @Override\n",
    "    public void execute() {\n",
    "        Operations.show(\"Twist\");\n",
    "    }\n",
    "}\n",
    "\n",
    "public class Machine {\n",
    "    public static void main(String[] args) {\n",
    "        Operations.runOps(\n",
    "        \tnew Bing(), new Crack(), new Twist());\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Bing\n",
    "Crack\n",
    "Twist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里展示了创建 **Operations** 的不同方式：一个外部类(Bing)，一个匿名类，一个方法引用和 lambda 表达式——毫无疑问用在这里是最好的解决方法。\n",
    "\n",
    "这个特性是一项改善，因为它允许把静态方法放在更合适的地方。\n",
    "\n",
    "### Instrument 作为接口\n",
    "\n",
    "回顾下乐器的例子，使用接口的话：\n",
    "\n",
    "![类图](../images/1562737974623.png)\n",
    "\n",
    "类 **Woodwind** 和 **Brass** 说明一旦实现了某个接口，那么其实现就变成一个普通类，可以按常规方式扩展它。\n",
    "\n",
    "接口的工作方式使得我们不需要显式声明其中的方法为 **public**，它们自动就是 **public** 的。`play()` 和 `adjust()` 使用 **default** 关键字定义实现。在 Java 8 之前，这些定义要在每个实现中重复实现，显得多余且令人烦恼："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/music5/Music5.java\n",
    "// {java interfaces.music5.Music5}\n",
    "package interfaces.music5;\n",
    "import polymorphism.music.Note;\n",
    "\n",
    "interface Instrument {\n",
    "    // Compile-time constant:\n",
    "    int VALUE = 5; // static & final\n",
    "    \n",
    "    default void play(Note n)  // Automatically public \n",
    "        System.out.println(this + \".play() \" + n);\n",
    "    }\n",
    "    \n",
    "    default void adjust() {\n",
    "        System.out.println(\"Adjusting \" + this);\n",
    "    }\n",
    "}\n",
    "\n",
    "class Wind implements Instrument {\n",
    "    @Override\n",
    "    public String toString() {\n",
    "        return \"Wind\";\n",
    "    }\n",
    "}\n",
    "\n",
    "class Percussion implements Instrument {\n",
    "    @Override\n",
    "    public String toString() {\n",
    "        return \"Percussion\";\n",
    "    }\n",
    "}\n",
    "\n",
    "class Stringed implements Instrument {\n",
    "    @Override\n",
    "    public String toString() {\n",
    "        return \"Stringed\";\n",
    "    }\n",
    "}\n",
    "\n",
    "class Brass extends Wind {\n",
    "    @Override\n",
    "    public String toString() {\n",
    "        return \"Brass\";\n",
    "    }\n",
    "}\n",
    "\n",
    "class Woodwind extends Wind {\n",
    "    @Override\n",
    "    public String toString() {\n",
    "        return \"Woodwind\";\n",
    "    }\n",
    "}\n",
    "\n",
    "public class Music5 {\n",
    "    // Doesn't care about type, so new types\n",
    "    // added to the system still work right:\n",
    "    static void tune(Instrument i) {\n",
    "        // ...\n",
    "        i.play(Note.MIDDLE_C);\n",
    "    }\n",
    "    \n",
    "    static void tuneAll(Instrument[] e) {\n",
    "        for (Instrument i: e) {\n",
    "            tune(i);\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    public static void main(String[] args) {\n",
    "        // Upcasting during addition to the array:\n",
    "        Instrument[] orchestra = {\n",
    "            new Wind(),\n",
    "            new Percussion(),\n",
    "            new Stringed(),\n",
    "            new Brass(),\n",
    "            new Woodwind()\n",
    "        }\n",
    "        tuneAll(orchestra);\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Wind.play() MIDDLE_C\n",
    "Percussion.play() MIDDLE_C\n",
    "Stringed.play() MIDDLE_C\n",
    "Brass.play() MIDDLE_C\n",
    "Woodwind.play() MIDDLE_C"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这个版本的例子的另一个变化是：`what()` 被修改为 `toString()` 方法，因为 `toString()` 实现的正是 `what()` 方法要实现的逻辑。因为 `toString()` 是根基类 **Object** 的方法，所以它不需要出现在接口中。\n",
    "\n",
    "注意到，无论是将其向上转型为称作 **Instrument** 的普通类，或称作 **Instrument** 的抽象类，还是叫作 **Instrument** 的接口，其行为都是相同的。事实上，从 `tune()` 方法上看不出来 **Instrument** 到底是一个普通类、抽象类，还是一个接口。\n",
    "\n",
    "<!-- Abstract Classes vs. Interfaces -->\n",
    "\n",
    "## 抽象类和接口\n",
    "\n",
    "尤其是在 Java 8 引入 **default** 方法之后，选择用抽象类还是用接口变得更加令人困惑。下表做了明确的区分：\n",
    "\n",
    "|         特性         |                            接口                            |                  抽象类                  |\n",
    "| :------------------: | :--------------------------------------------------------: | :--------------------------------------: |\n",
    "|         组合         |                    新类可以组合多个接口                    |            只能继承单一抽象类            |\n",
    "|         状态         |        不能包含属性（除了静态属性，不支持对象状态）        | 可以包含属性，非抽象方法可能引用这些属性 |\n",
    "| 默认方法 和 抽象方法 | 不需要在子类中实现默认方法。默认方法可以引用其他接口的方法 |         必须在子类中实现抽象方法         |\n",
    "|        构造器        |                         没有构造器                         |               可以有构造器               |\n",
    "|        可见性        |                      隐式 **public**                       |       可以是 **protected** 或友元        |\n",
    "\n",
    "抽象类仍然是一个类，在创建新类时只能继承它一个。而创建类的过程中可以实现多个接口。\n",
    "\n",
    "有一条实际经验：尽可能地抽象。因此，更倾向使用接口而不是抽象类。只有当必要时才使用抽象类。除非必须使用，否则不要用接口和抽象类。大多数时候，普通类已经做得很好，如果不行的话，再移动到接口或抽象类中。\n",
    "\n",
    "<!-- Complete Decoupling -->\n",
    "\n",
    "## 完全解耦\n",
    "\n",
    "当方法操纵的是一个类而非接口时，它就只能作用于那个类或其子类。如果想把方法应用于那个继承层级结构之外的类，就会触霉头。接口在很大程度上放宽了这个限制，因而使用接口可以编写复用性更好的代码。\n",
    "\n",
    "例如有一个类 **Process** 有两个方法 `name()` 和 `process()`。`process()` 方法接受输入，修改并输出。把这个类作为基类用来创建各种不同类型的 **Processor**。下例中，**Processor** 的各个子类修改 String 对象（注意，返回类型可能是协变类型而非参数类型）："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/Applicator.java\n",
    "import java.util.*;\n",
    "\n",
    "class Processor {\n",
    "    public String name() {\n",
    "        return getClass().getSimpleName();\n",
    "    }\n",
    "    \n",
    "    public Object process(Object input) {\n",
    "        return input;\n",
    "    }\n",
    "}\n",
    "\n",
    "class Upcase extends Processor {\n",
    "    // 返回协变类型\n",
    "    @Override \n",
    "    public String process(Object input) {\n",
    "        return ((String) input).toUpperCase();\n",
    "    }\n",
    "}\n",
    "\n",
    "class Downcase extends Processor {\n",
    "    @Override\n",
    "    public String process(Object input) {\n",
    "        return ((String) input).toLowerCase();\n",
    "    }\n",
    "}\n",
    "\n",
    "class Splitter extends Processor {\n",
    "    @Override\n",
    "    public String process(Object input) {\n",
    "        // split() divides a String into pieces:\n",
    "        return Arrays.toString(((String) input).split(\" \"));\n",
    "    }\n",
    "}\n",
    "\n",
    "public class Applicator {\n",
    "    public static void apply(Processor p, Object s) {\n",
    "        System.out.println(\"Using Processor \" + p.name());\n",
    "        System.out.println(p.process(s));\n",
    "    }\n",
    "    \n",
    "    public static void main(String[] args) {\n",
    "        String s = \"We are such stuff as dreams are made on\";\n",
    "        apply(new Upcase(), s);\n",
    "        apply(new Downcase(), s);\n",
    "        apply(new Splitter(), s);\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Using Processor Upcase\n",
    "WE ARE SUCH STUFF AS DREAMS ARE MADE ON\n",
    "Using Processor Downcase\n",
    "we are such stuff as dreams are made on\n",
    "Using Processor Splitter\n",
    "[We, are, such, stuff, as, dreams, are, made, on]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Applicator** 的 `apply()` 方法可以接受任何类型的 **Processor**，并将其应用到一个 **Object** 对象上输出结果。像本例中这样，创建一个能根据传入的参数类型从而具备不同行为的方法称为*策略*设计模式。方法包含算法中不变的部分，策略包含变化的部分。策略就是传入的对象，它包含要执行的代码。在这里，**Processor** 对象是策略，`main()` 方法展示了三种不同的应用于 **String s** 上的策略。\n",
    "\n",
    "`split()` 是 **String** 类中的方法，它接受 **String** 类型的对象并以传入的参数作为分割界限，返回一个数组 **String[]**。在这里用它是为了更快地创建 **String** 数组。\n",
    "\n",
    "假设现在发现了一组电子滤波器，它们看起来好像能使用 **Applicator** 的 `apply()` 方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/filters/Waveform.java\n",
    "package interfaces.filters;\n",
    "\n",
    "public class Waveform {\n",
    "    private static long counter;\n",
    "    private final long id = counter++;\n",
    "    \n",
    "    @Override\n",
    "    public String toString() {\n",
    "        return \"Waveform \" + id;\n",
    "    }\n",
    "}\n",
    "\n",
    "// interfaces/filters/Filter.java\n",
    "package interfaces.filters;\n",
    "\n",
    "public class Filter {\n",
    "    public String name() {\n",
    "        return getClass().getSimpleName();\n",
    "    }\n",
    "    \n",
    "    public Waveform process(Waveform input) {\n",
    "        return input;\n",
    "    }\n",
    "}\n",
    "\n",
    "// interfaces/filters/LowPass.java\n",
    "package interfaces.filters;\n",
    "\n",
    "public class LowPass extends Filter {\n",
    "    double cutoff;\n",
    "    \n",
    "    public LowPass(double cutoff) {\n",
    "        this.cutoff = cutoff;\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public Waveform process(Waveform input) {\n",
    "        return input; // Dummy processing 哑处理\n",
    "    }\n",
    "}\n",
    "\n",
    "// interfaces/filters/HighPass.java\n",
    "package interfaces.filters;\n",
    "\n",
    "public class HighPass extends Filter {\n",
    "    double cutoff;\n",
    "    \n",
    "    public HighPass(double cutoff) {\n",
    "        this.cutoff = cutoff;\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public Waveform process(Waveform input) {\n",
    "        return input;\n",
    "    }\n",
    "}\n",
    "\n",
    "// interfaces/filters/BandPass.java\n",
    "package interfaces.filters;\n",
    "\n",
    "public class BandPass extends Filter {\n",
    "    double lowCutoff, highCutoff;\n",
    "    \n",
    "    public BandPass(double lowCut, double highCut) {\n",
    "        lowCutoff = lowCut;\n",
    "        highCutoff = highCut;\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public Waveform process(Waveform input) {\n",
    "        return input;\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Filter** 类与 **Processor** 类具有相同的接口元素，但是因为它不是继承自 **Processor** —— 因为 **Filter** 类的创建者根本不知道你想将它当作 **Processor** 使用 —— 因此你不能将 **Applicator** 的 `apply()` 方法应用在 **Filter** 类上，即使这样做也能正常运行。主要是因为 **Applicator** 的 `apply()` 方法和 **Processor** 过于耦合，这阻止了 **Applicator** 的 `apply()` 方法被复用。另外要注意的一点是 Filter 类中 `process()` 方法的输入输出都是 **Waveform**。\n",
    "\n",
    "但如果 **Processor** 是一个接口，那么限制就会变得松动到足以复用 **Applicator** 的 `apply()` 方法，用来接受那个接口参数。下面是修改后的 **Processor** 和 **Applicator** 版本："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/interfaceprocessor/Processor.java\n",
    "package interfaces.interfaceprocessor;\n",
    "\n",
    "public interface Processor {\n",
    "    default String name() {\n",
    "        return getClass().getSimpleName();\n",
    "    }\n",
    "    \n",
    "    Object process(Object input);\n",
    "}\n",
    "\n",
    "// interfaces/interfaceprocessor/Applicator.java\n",
    "package interfaces.interfaceprocessor;\n",
    "\n",
    "public class Applicator {\n",
    "    public static void apply(Processor p, Object s) {\n",
    "        System.out.println(\"Using Processor \" + p.name());\n",
    "        System.out.println(p.process(s));\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "复用代码的第一种方式是客户端程序员遵循接口编写类，像这样："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/interfaceprocessor/StringProcessor.java\n",
    "// {java interfaces.interfaceprocessor.StringProcessor}\n",
    "package interfaces.interfaceprocessor;\n",
    "import java.util.*;\n",
    "\n",
    "interface StringProcessor extends Processor {\n",
    "    @Override\n",
    "    String process(Object input); // [1]\n",
    "    String S = \"If she weighs the same as a duck, she's made of wood\"; // [2]\n",
    "    \n",
    "    static void main(String[] args) { // [3]\n",
    "        Applicator.apply(new Upcase(), S);\n",
    "        Applicator.apply(new Downcase(), S);\n",
    "        Applicator.apply(new Splitter(), S);\n",
    "    }\n",
    "}\n",
    "\n",
    "class Upcase implements StringProcessor {\n",
    "    // 返回协变类型\n",
    "    @Override\n",
    "    public String process(Object input) {\n",
    "        return ((String) input).toUpperCase();\n",
    "    }\n",
    "}\n",
    "\n",
    "class Downcase implements StringProcessor {\n",
    "    @Override\n",
    "    public String process(Object input) {\n",
    "        return ((String) input).toLowerCase();\n",
    "    }\n",
    "}\n",
    "\n",
    "class Splitter implements StringProcessor {\n",
    "    @Override\n",
    "    public String process(Object input) {\n",
    "        return Arrays.toString(((String) input).split(\" \"));\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Using Processor Upcase\n",
    "IF SHE WEIGHS THE SAME AS A DUCK, SHE'S MADE OF WOOD\n",
    "Using Processor Downcase\n",
    "if she weighs the same as a duck, she's made of wood\n",
    "Using Processor Splitter\n",
    "[If, she, weighs, the, same, as, a, duck,, she's, made, of, wood]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">[1] 该声明不是必要的，即使移除它，编译器也不会报错。但是注意这里的协变返回类型从 Object 变成了 String。\n",
    ">\n",
    ">[2] S 自动就是 final 和 static 的，因为它是在接口中定义的。\n",
    ">\n",
    ">[3] 可以在接口中定义 `main()` 方法。\n",
    "\n",
    "这种方式运作得很好，然而你经常遇到的情况是无法修改类。例如在电子滤波器的例子中，类库是被发现而不是创建的。在这些情况下，可以使用*适配器*设计模式。适配器允许代码接受已有的接口产生需要的接口，如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/interfaceprocessor/FilterProcessor.java\n",
    "// {java interfaces.interfaceprocessor.FilterProcessor}\n",
    "package interfaces.interfaceprocessor;\n",
    "import interfaces.filters.*;\n",
    "\n",
    "class FilterAdapter implements Processor {\n",
    "    Filter filter;\n",
    "    \n",
    "    FilterAdapter(Filter filter) {\n",
    "        this.filter = filter;\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public String name() {\n",
    "        return filter.name();\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public Waveform process(Object input) {\n",
    "        return filter.process((Waveform) input);\n",
    "    }\n",
    "}\n",
    "\n",
    "public class FilterProcessor {\n",
    "    public static void main(String[] args) {\n",
    "        Waveform w = new Waveform();\n",
    "        Applicator.apply(new FilterAdapter(new LowPass(1.0)), w);\n",
    "        Applicator.apply(new FilterAdapter(new HighPass(2.0)), w);\n",
    "        Applicator.apply(new FilterAdapter(new BandPass(3.0, 4.0)), w);\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Using Processor LowPass\n",
    "Waveform 0\n",
    "Using Processor HighPass\n",
    "Waveform 0\n",
    "Using Processor BandPass\n",
    "Waveform 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在这种使用适配器的方式中，**FilterAdapter** 的构造器接受已有的接口 **Filter**，继而产生需要的 **Processor** 接口的对象。你可能还注意到 **FilterAdapter** 中使用了委托。\n",
    "\n",
    "协变允许我们从 `process()` 方法中产生一个 **Waveform** 而非 **Object** 对象。\n",
    "\n",
    "将接口与实现解耦使得接口可以应用于多种不同的实现，因而代码更具可复用性。\n",
    "\n",
    "<!-- Combining Multiple Interfaces -->\n",
    "\n",
    "## 多接口结合\n",
    "\n",
    "接口没有任何实现——也就是说，没有任何与接口相关的存储——因此无法阻止结合的多接口。这是有价值的，因为你有时需要表示“一个 **x** 是一个 **a** 和一个 **b** 以及一个 **c**”。\n",
    "\n",
    "![类图](../images/1562999314238.png)\n",
    "\n",
    "派生类并不要求必须继承自抽象的或“具体的”（没有任何抽象方法）的基类。如果继承一个非接口的类，那么只能继承一个类，其余的基元素必须都是接口。需要将所有的接口名称置于 **implements** 关键字之后且用逗号分隔。可以有任意多个接口，并可以向上转型为每个接口，因为每个接口都是独立的类型。下例展示了一个由多个接口组合而成的具体类产生的新类："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/Adventure.java\n",
    "// Multiple interfaces\n",
    "interface CanFight {\n",
    "    void fight();\n",
    "}\n",
    "\n",
    "interface CanSwim {\n",
    "    void swim();\n",
    "}\n",
    "\n",
    "interface CanFly {\n",
    "    void fly();\n",
    "}\n",
    "\n",
    "class ActionCharacter {\n",
    "    public void fight(){}\n",
    "}\n",
    "\n",
    "class Hero extends ActionCharacter implements CanFight, CanSwim, CanFly {\n",
    "    public void swim() {}\n",
    "    \n",
    "    public void fly() {}\n",
    "}\n",
    "\n",
    "public class Adventure {\n",
    "    public static void t(CanFight x) {\n",
    "        x.fight();\n",
    "    }\n",
    "    \n",
    "    public static void u(CanSwim x) {\n",
    "        x.swim();\n",
    "    }\n",
    "    \n",
    "    public static void v(CanFly x) {\n",
    "        x.fly();\n",
    "    }\n",
    "    \n",
    "    public static void w(ActionCharacter x) {\n",
    "        x.fight();\n",
    "    }\n",
    "    \n",
    "    public static void main(String[] args) {\n",
    "        Hero h = new Hero();\n",
    "        t(h); // Treat it as a CanFight\n",
    "        u(h); // Treat it as a CanSwim\n",
    "        v(h); // Treat it as a CanFly\n",
    "        w(h); // Treat it as an ActionCharacter\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "类 **Hero** 结合了具体类 **ActionCharacter** 和接口 **CanFight**、**CanSwim** 和 **CanFly**。当通过这种方式结合具体类和接口时，需要将具体类放在前面，后面跟着接口（否则编译器会报错）。\n",
    "\n",
    "接口 **CanFight** 和类 **ActionCharacter** 中的 `fight()` 方法签名相同，而在类 Hero 中也没有提供 `fight()` 的定义。可以扩展一个接口，但是得到的是另一个接口。当想创建一个对象时，所有的定义必须首先都存在。类 **Hero** 中没有显式地提供 `fight()` 的定义，是由于该方法在类 **ActionCharacter** 中已经定义过，这样才使得创建 **Hero** 对象成为可能。\n",
    "\n",
    "在类 **Adventure** 中可以看到四个方法，它们把不同的接口和具体类作为参数。当创建一个 **Hero** 对象时，它可以被传入这些方法中的任意一个，意味着它可以依次向上转型为每个接口。Java 中这种接口的设计方式，使得程序员不需要付出特别的努力。\n",
    "\n",
    "记住，前面例子展示了使用接口的核心原因之一：为了能够向上转型为多个基类型（以及由此带来的灵活性）。然而，使用接口的第二个原因与使用抽象基类相同：防止客户端程序员创建这个类的对象，确保这仅仅只是一个接口。这带来了一个问题：应该使用接口还是抽象类呢？如果创建不带任何方法定义或成员变量的基类，就选择接口而不是抽象类。事实上，如果知道某事物是一个基类，可以考虑用接口实现它（这个主题在本章总结会再次讨论）。\n",
    "\n",
    "<!-- Extending an Interface with Inheritance -->\n",
    "\n",
    "## 使用继承扩展接口\n",
    "\n",
    "通过继承，可以很容易在接口中增加方法声明，还可以在新接口中结合多个接口。这两种情况都可以得到新接口，如下例所示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/HorrorShow.java\n",
    "// Extending an interface with inheritance\n",
    "interface Monster {\n",
    "    void menace();\n",
    "}\n",
    "\n",
    "interface DangerousMonster extends Monster {\n",
    "    void destroy();\n",
    "}\n",
    "\n",
    "interface Lethal {\n",
    "    void kill();\n",
    "}\n",
    "\n",
    "class DragonZilla implements DangerousMonster {\n",
    "    @Override\n",
    "    public void menace() {}\n",
    "    \n",
    "    @Override\n",
    "    public void destroy() {}\n",
    "}\n",
    "\n",
    "interface Vampire extends DangerousMonster, Lethal {\n",
    "    void drinkBlood();\n",
    "}\n",
    "\n",
    "class VeryBadVampire implements Vampire {\n",
    "    @Override\n",
    "    public void menace() {}\n",
    "    \n",
    "    @Override\n",
    "    public void destroy() {}\n",
    "    \n",
    "    @Override\n",
    "    public void kill() {}\n",
    "    \n",
    "    @Override\n",
    "    public void drinkBlood() {}\n",
    "}\n",
    "\n",
    "public class HorrorShow {\n",
    "    static void u(Monster b) {\n",
    "        b.menace();\n",
    "    }\n",
    "    \n",
    "    static void v(DangerousMonster d) {\n",
    "        d.menace();\n",
    "        d.destroy();\n",
    "    }\n",
    "    \n",
    "    static void w(Lethal l) {\n",
    "        l.kill();\n",
    "    }\n",
    "    \n",
    "    public static void main(String[] args) {\n",
    "        DangerousMonster barney = new DragonZilla();\n",
    "        u(barney);\n",
    "        v(barney);\n",
    "        Vampire vlad = new VeryBadVampire();\n",
    "        u(vlad);\n",
    "        v(vlad);\n",
    "        w(vlad);\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "接口 **DangerousMonster** 是 **Monster** 简单扩展的一个新接口，类 **DragonZilla** 实现了这个接口。\n",
    "\n",
    "**Vampire** 中使用的语法仅适用于接口继承。通常来说，**extends** 只能用于单一类，但是在构建接口时可以引用多个基类接口。注意到，接口名之间用逗号分隔。\n",
    "\n",
    "### 结合接口时的命名冲突\n",
    "\n",
    "当实现多个接口时可能会存在一个小陷阱。在前面的例子中，**CanFight** 和 **ActionCharacter** 具有完全相同的 `fight()` 方法。完全相同的方法没有问题，但是如果它们的签名或返回类型不同会怎么样呢？这里有一个例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/InterfaceCollision.java\n",
    "interface I1 {\n",
    "    void f();\n",
    "}\n",
    "\n",
    "interface I2 {\n",
    "    int f(int i);\n",
    "}\n",
    "\n",
    "interface I3 {\n",
    "    int f();\n",
    "}\n",
    "\n",
    "class C {\n",
    "    public int f() {\n",
    "        return 1;\n",
    "    }\n",
    "}\n",
    "\n",
    "class C2 implements I1, I2 {\n",
    "    @Override\n",
    "    public void f() {}\n",
    "    \n",
    "    @Override\n",
    "    public int f(int i) {\n",
    "        return 1;  // 重载\n",
    "    }\n",
    "}\n",
    "\n",
    "class C3 extends C implements I2 {\n",
    "    @Override\n",
    "    public int f(int i) {\n",
    "        return 1; // 重载\n",
    "    }\n",
    "}\n",
    "\n",
    "class C4 extends C implements I3 {\n",
    "    // 完全相同，没问题\n",
    "    @Override\n",
    "    public int f() {\n",
    "        return 1;\n",
    "    }\n",
    "}\n",
    "\n",
    "// 方法的返回类型不同\n",
    "//- class C5 extends C implements I1 {}\n",
    "//- interface I4 extends I1, I3 {}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "覆写、实现和重载令人不快地搅和在一起带来了困难。同时，重载方法仅根据返回类型是区分不了的。当不注释最后两行时，报错信息如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "error: C5 is not abstract and does not override abstract\n",
    "method f() in I1\n",
    "class C5 extends C implements I1 {}\n",
    "error: types I3 and I1 are incompatible; both define f(),\n",
    "but with unrelated return types\n",
    "interfacce I4 extends I1, I3 {}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当打算组合接口时，在不同的接口中使用相同的方法名通常会造成代码可读性的混乱，尽量避免这种情况。\n",
    "\n",
    "<!-- Adapting to an Interface -->\n",
    "\n",
    "## 接口适配\n",
    "\n",
    "接口最吸引人的原因之一是相同的接口可以有多个实现。在简单情况下体现在一个方法接受接口作为参数，该接口的实现和传递对象给方法则交由你来做。\n",
    "\n",
    "因此，接口的一种常见用法是前面提到的*策略*设计模式。编写一个方法执行某些操作并接受一个指定的接口作为参数。可以说：“只要对象遵循接口，就可以调用方法” ，这使得方法更加灵活，通用，并更具可复用性。\n",
    "\n",
    "例如，类 **Scanner** 的构造器接受的是一个 **Readable** 接口（在“字符串”一章中学习更多相关内容）。你会发现 **Readable** 没有用作 Java 标准库中其他任何方法的参数——它是单独为 **Scanner** 创建的，因此 **Scanner** 没有将其参数限制为某个特定类。通过这种方式，**Scanner** 可以与更多的类型协作。如果你创建了一个新类并想让 **Scanner** 作用于它，就让它实现 **Readable** 接口，像这样："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/RandomStrings.java\n",
    "// Implementing an interface to conform to a method\n",
    "import java.nio.*;\n",
    "import java.util.*;\n",
    "\n",
    "public class RandomStrings implements Readable {\n",
    "    private static Random rand = new Random(47);\n",
    "    private static final char[] CAPITALS = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\".toCharArray();\n",
    "    private static final char[] LOWERS = \"abcdefghijklmnopqrstuvwxyz\".toCharArray();\n",
    "    private static final char[] VOWELS = \"aeiou\".toCharArray();\n",
    "    private int count;\n",
    "    \n",
    "    public RandomStrings(int count) {\n",
    "        this.count = count;\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public int read(CharBuffer cb) {\n",
    "        if (count-- == 0) {\n",
    "            return -1; // indicates end of input\n",
    "        }\n",
    "        cb.append(CAPITALS[rand.nextInt(CAPITALS.length)]);\n",
    "        for (int i = 0; i < 4; i++) {\n",
    "            cb.append(VOWELS[rand.nextInt(VOWELS.length)]);\n",
    "            cb.append(LOWERS[rand.nextInt(LOWERS.length)]);\n",
    "        }\n",
    "        cb.append(\" \");\n",
    "        return 10; // Number of characters appended\n",
    "    }\n",
    "    \n",
    "    public static void main(String[] args) {\n",
    "        Scanner s = new Scanner(new RandomStrings(10));\n",
    "        while (s.hasNext()) {\n",
    "            System.out.println(s.next());\n",
    "        }\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Yazeruyac\n",
    "Fowenucor\n",
    "Goeazimom\n",
    "Raeuuacio\n",
    "Nuoadesiw\n",
    "Hageaikux\n",
    "Ruqicibui\n",
    "Numasetih\n",
    "Kuuuuozog\n",
    "Waqizeyoy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Readable** 接口只需要实现 `read()` 方法（注意 `@Override` 注解的突出方法）。在 `read()` 方法里，将输入内容添加到 **CharBuffer** 参数中（有多种方法可以实现，查看 **CharBuffer** 文档），或在没有输入时返回 **-1**。\n",
    "\n",
    "假设你有一个类没有实现 **Readable** 接口，怎样才能让 **Scanner** 作用于它呢？下面是一个产生随机浮点数的例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/RandomDoubles.java\n",
    "import java.util.*;\n",
    "\n",
    "public interface RandomDoubles {\n",
    "    Random RAND = new Random(47);\n",
    "    \n",
    "    default double next() {\n",
    "        return RAND.nextDouble();\n",
    "    }\n",
    "    \n",
    "    static void main(String[] args) {\n",
    "        RandomDoubles rd = new RandomDoubles(){};\n",
    "        for (int i = 0; i < 7; i++) {\n",
    "            System.out.println(rd.next() + \" \");\n",
    "        }\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "0.7271157860730044 \n",
    "0.5309454508634242 \n",
    "0.16020656493302599 \n",
    "0.18847866977771732 \n",
    "0.5166020801268457 \n",
    "0.2678662084200585 \n",
    "0.2613610344283964"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以再次使用适配器模式，但这里适配器类可以实现两个接口。因此，通过关键字 **interface** 提供的多继承，我们可以创建一个既是 **RandomDoubles**，又是 **Readable** 的类："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/AdaptedRandomDoubles.java\n",
    "// creating an adapter with inheritance\n",
    "import java.nio.*;\n",
    "import java.util.*;\n",
    "\n",
    "public class AdaptedRandomDoubles implements RandomDoubles, Readable {\n",
    "    private int count;\n",
    "    \n",
    "    public AdaptedRandomDoubles(int count) {\n",
    "        this.count = count;\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public int read(CharBuffer cb) {\n",
    "        if (count-- == 0) {\n",
    "            return -1;\n",
    "        }\n",
    "        String result = Double.toString(next()) + \" \";\n",
    "        cb.append(result);\n",
    "        return result.length();\n",
    "    }\n",
    "    \n",
    "    public static void main(String[] args) {\n",
    "        Scanner s = new Scanner(new AdaptedRandomDoubles(7));\n",
    "        while (s.hasNextDouble()) {\n",
    "            System.out.print(s.nextDouble() + \" \");\n",
    "        }\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "0.7271157860730044 0.5309454508634242 \n",
    "0.16020656493302599 0.18847866977771732 \n",
    "0.5166020801268457 0.2678662084200585 \n",
    "0.2613610344283964"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "因为你可以以这种方式在已有类中增加新接口，所以这就意味着一个接受接口类型的方法提供了一种让任何类都可以与该方法进行适配的方式。这就是使用接口而不是类的强大之处。\n",
    "\n",
    "<!-- Fields in Interfaces -->\n",
    "\n",
    "## 接口字段\n",
    "\n",
    "因为接口中的字段都自动是 **static** 和 **final** 的，所以接口就成为了创建一组常量的方便的工具。在 Java 5 之前，这是产生与 C 或 C++ 中的 enum (枚举类型) 具有相同效果的唯一方式。所以你可能在 Java 5 之前的代码中看到："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/Months.java\n",
    "// Using interfaces to create groups of constants\n",
    "public interface Months {\n",
    "    int \n",
    "    JANUARY = 1, FEBRUARY = 2, MARCH = 3,\n",
    "    APRIL = 4, MAY = 5, JUNE = 6, JULY = 7,\n",
    "    AUGUST = 8, SEPTEMBER = 9, OCTOBER = 10,\n",
    "    NOVEMBER = 11, DECEMBER = 12;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意 Java 中使用大写字母的风格定义具有初始化值的 **static** **final** 变量。接口中的字段自动是 **public** 的，所以没有显式指明这点。\n",
    "\n",
    "自 Java 5 开始，我们有了更加强大和灵活的关键字 **enum**，那么在接口中定义常量组就显得没什么意义了。然而当你阅读遗留的代码时，在很多场合你还会碰到这种旧的习惯用法。在“枚举”一章中你会学习到更多关于枚举的内容。\n",
    "\n",
    "### 初始化接口中的字段\n",
    "\n",
    "接口中定义的字段不能是“空 **final**\"，但是可以用非常量表达式初始化。例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/RandVals.java\n",
    "// Initializing interface fields with\n",
    "// non-constant initializers\n",
    "import java.util.*;\n",
    "\n",
    "public interface RandVals {\n",
    "    Random RAND = new Random(47);\n",
    "    int RANDOM_INT = RAND.nextInt(10);\n",
    "    long RANDOM_LONG = RAND.nextLong() * 10;\n",
    "    float RANDOM_FLOAT = RAND.nextLong() * 10;\n",
    "    double RANDOM_DOUBLE = RAND.nextDouble() * 10;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "因为字段是 **static** 的，所以它们在类第一次被加载时初始化，这发生在任何字段首次被访问时。下面是个简单的测试："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/TestRandVals.java\n",
    "public class TestRandVals {\n",
    "    public static void main(String[] args) {\n",
    "        System.out.println(RandVals.RANDOM_INT);\n",
    "        System.out.println(RandVals.RANDOM_LONG);\n",
    "        System.out.println(RandVals.RANDOM_FLOAT);\n",
    "        System.out.println(RandVals.RANDOM_DOUBLE);\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "8\n",
    "-32032247016559954\n",
    "-8.5939291E18\n",
    "5.779976127815049"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这些字段不是接口的一部分，它们的值被存储在接口的静态存储区域中。\n",
    "\n",
    "<!-- Nesting Interfaces -->\n",
    "\n",
    "## 接口嵌套\n",
    "\n",
    "接口可以嵌套在类或其他接口中。下面揭示一些有趣的特性："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/nesting/NestingInterfaces.java\n",
    "// {java interfaces.nesting.NestingInterfaces}\n",
    "package interfaces.nesting;\n",
    "\n",
    "class A {\n",
    "    interface B {\n",
    "        void f();\n",
    "    }\n",
    "    \n",
    "    public class BImp implements B {\n",
    "        @Override\n",
    "        public void f() {}\n",
    "    }\n",
    "    \n",
    "    public class BImp2 implements B {\n",
    "        @Override\n",
    "        public void f() {}\n",
    "    }\n",
    "    \n",
    "    public interface C {\n",
    "        void f();\n",
    "    }\n",
    "    \n",
    "    class CImp implements C {\n",
    "        @Override\n",
    "        public void f() {}\n",
    "    }\n",
    "    \n",
    "    private class CImp2 implements C {\n",
    "        @Override\n",
    "        public void f() {}\n",
    "    }\n",
    "    \n",
    "    private interface D {\n",
    "        void f();\n",
    "    }\n",
    "    \n",
    "    private class DImp implements D {\n",
    "        @Override\n",
    "        public void f() {}\n",
    "    }\n",
    "    \n",
    "    public class DImp2 implements D {\n",
    "        @Override\n",
    "        public void f() {}\n",
    "    }\n",
    "    \n",
    "    public D getD() {\n",
    "        return new DImp2();\n",
    "    }\n",
    "    \n",
    "    private D dRef;\n",
    "    \n",
    "    public void receiveD(D d) {\n",
    "        dRef = d;\n",
    "        dRef.f();\n",
    "    }\n",
    "}\n",
    "\n",
    "interface E {\n",
    "    interface G {\n",
    "        void f();\n",
    "    }\n",
    "    // Redundant \"public\"\n",
    "    public interface H {\n",
    "        void f();\n",
    "    }\n",
    "    \n",
    "    void g();\n",
    "    // Cannot be private within an interface\n",
    "    //- private interface I {}\n",
    "}\n",
    "\n",
    "public class NestingInterfaces {\n",
    "    public class BImp implements A.B {\n",
    "        @Override\n",
    "        public void f() {}\n",
    "    }\n",
    "    \n",
    "    class CImp implements A.C {\n",
    "        @Override\n",
    "        public void f() {}\n",
    "    }\n",
    "    // Cannot implements a private interface except\n",
    "    // within that interface's defining class:\n",
    "    //- class DImp implements A.D {\n",
    "    //- public void f() {}\n",
    "    //- }\n",
    "    class EImp implements E {\n",
    "        @Override\n",
    "        public void g() {}\n",
    "    }\n",
    "    \n",
    "    class EGImp implements E.G {\n",
    "        @Override\n",
    "        public void f() {}\n",
    "    }\n",
    "    \n",
    "    class EImp2 implements E {\n",
    "        @Override\n",
    "        public void g() {}\n",
    "        \n",
    "        class EG implements E.G {\n",
    "            @Override\n",
    "            public void f() {}\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    public static void main(String[] args) {\n",
    "        A a = new A();\n",
    "        // Can't access to A.D:\n",
    "        //- A.D ad = a.getD();\n",
    "        // Doesn't return anything but A.D:\n",
    "        //- A.DImp2 di2 = a.getD();\n",
    "        // cannot access a member of the interface:\n",
    "        //- a.getD().f();\n",
    "        // Only another A can do anything with getD():\n",
    "        A a2 = new A();\n",
    "        a2.receiveD(a.getD());\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在类中嵌套接口的语法是相当显而易见的。就像非嵌套接口一样，它们具有 **public** 或包访问权限的可见性。\n",
    "\n",
    "作为一种新添加的方式，接口也可以是 **private** 的，例如 **A.D**（同样的语法同时适用于嵌套接口和嵌套类）。那么 **private** 嵌套接口有什么好处呢？你可能猜测它只是被用来实现一个 **private** 内部类，就像 **DImp**。然而 **A.DImp2** 展示了它可以被实现为 **public** 类，但是 **A.DImp2** 只能被自己使用，你无法说它实现了 **private** 接口 **D**，所以实现 **private** 接口是一种可以强制该接口中的方法定义不会添加任何类型信息（即不可以向上转型）的方式。\n",
    "\n",
    "`getD()` 方法产生了一个与 **private** 接口有关的窘境。它是一个 **public** 方法却返回了对 **private** 接口的引用。能对这个返回值做些什么呢？`main()` 方法里进行了一些使用返回值的尝试但都失败了。返回值必须交给有权使用它的对象，本例中另一个 **A** 通过 `receiveD()` 方法接受了它。\n",
    "\n",
    "接口 **E** 说明了接口之间也能嵌套。然而，作用于接口的规则——尤其是，接口中的元素必须是 **public** 的——在此都会被严格执行，所以嵌套在另一个接口中的接口自动就是 **public** 的，不能指明为 **private**。\n",
    "\n",
    "类 **NestingInterfaces** 展示了嵌套接口的不同实现方式。尤其是当实现某个接口时，并不需要实现嵌套在其内部的接口。同时，**private** 接口不能在定义它的类之外被实现。\n",
    "\n",
    "添加这些特性的最初原因看起来像是出于对严格的语法一致性的考虑，但是我通常认为，一旦你了解了某种特性，就总能找到其用武之地。\n",
    "\n",
    "<!-- Interfaces and Factories -->\n",
    "\n",
    "## 接口和工厂方法模式\n",
    "\n",
    "接口是多实现的途径，而生成符合某个接口的对象的典型方式是*工厂方法*设计模式。不同于直接调用构造器，只需调用工厂对象中的创建方法就能生成对象的实现——理论上，通过这种方式可以将接口与实现的代码完全分离，使得可以透明地将某个实现替换为另一个实现。这里是一个展示工厂方法结构的例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/Factories.java\n",
    "interface Service {\n",
    "    void method1();\n",
    "    void method2();\n",
    "}\n",
    "\n",
    "interface ServiceFactory {\n",
    "    Service getService();\n",
    "}\n",
    "\n",
    "class Service1 implements Service {\n",
    "    Service1() {} // Package access\n",
    "    \n",
    "    @Override\n",
    "    public void method1() {\n",
    "        System.out.println(\"Service1 method1\");\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public void method2() {\n",
    "        System.out.println(\"Service1 method2\");\n",
    "    }\n",
    "}\n",
    "\n",
    "class Service1Factory implements ServiceFactory {\n",
    "    @Override\n",
    "    public Service getService() {\n",
    "        return new Service1();\n",
    "    }\n",
    "}\n",
    "\n",
    "class Service2 implements Service {\n",
    "    Service2() {} // Package access\n",
    "    \n",
    "    @Override\n",
    "    public void method1() {\n",
    "        System.out.println(\"Service2 method1\");\n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public void method2() {\n",
    "        System.out.println(\"Service2 method2\");\n",
    "    }\n",
    "}\n",
    "\n",
    "class Service2Factory implements ServiceFactory {\n",
    "    @Override\n",
    "    public Service getService() {\n",
    "        return new Service2();\n",
    "    }\n",
    "}\n",
    "\n",
    "public class Factories {\n",
    "    public static void serviceConsumer(ServiceFactory fact) {\n",
    "        Service s = fact.getService();\n",
    "        s.method1();\n",
    "        s.method2();\n",
    "    }\n",
    "    \n",
    "    public static void main(String[] args) {\n",
    "        serviceConsumer(new Service1Factory());\n",
    "        // Services are completely interchangeable:\n",
    "        serviceConsumer(new Service2Factory());\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Service1 method1\n",
    "Service1 method2\n",
    "Service2 method1\n",
    "Service2 method2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果没有工厂方法，代码就必须在某处指定将要创建的 **Service** 的确切类型，从而调用恰当的构造器。\n",
    "\n",
    "为什么要添加额外的间接层呢？一个常见的原因是创建框架。假设你正在创建一个游戏系统；例如，在相同的棋盘下国际象棋和西洋跳棋："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "java"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "// interfaces/Games.java\n",
    "// A Game framework using Factory Methods\n",
    "interface Game {\n",
    "    boolean move();\n",
    "}\n",
    "\n",
    "interface GameFactory {\n",
    "    Game getGame();\n",
    "}\n",
    "\n",
    "class Checkers implements Game {\n",
    "    private int moves = 0;\n",
    "    private static final int MOVES = 3;\n",
    "    \n",
    "    @Override\n",
    "    public boolean move() {\n",
    "        System.out.println(\"Checkers move \" + moves);\n",
    "        return ++moves != MOVES;\n",
    "    }\n",
    "}\n",
    "\n",
    "class CheckersFactory implements GameFactory {\n",
    "    @Override\n",
    "    public Game getGame() {\n",
    "        return new Checkers();\n",
    "    }\n",
    "}\n",
    "\n",
    "class Chess implements Game {\n",
    "    private int moves = 0;\n",
    "    private static final int MOVES = 4;\n",
    "    \n",
    "    @Override\n",
    "    public boolean move() {\n",
    "        System.out.println(\"Chess move \" + moves);\n",
    "        return ++moves != MOVES;\n",
    "    }\n",
    "}\n",
    "\n",
    "class ChessFactory implements GameFactory {\n",
    "    @Override\n",
    "    public Game getGame() {\n",
    "        return new Chess();\n",
    "    }\n",
    "}\n",
    "\n",
    "public class Games {\n",
    "    public static void playGame(GameFactory factory) {\n",
    "        Game s = factory.getGame();\n",
    "        while (s.move()) {\n",
    "            ;\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    public static void main(String[] args) {\n",
    "        playGame(new CheckersFactory());\n",
    "        playGame(new ChessFactory());\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Checkers move 0\n",
    "Checkers move 1\n",
    "Checkers move 2\n",
    "Chess move 0\n",
    "Chess move 1\n",
    "Chess move 2\n",
    "Chess move 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果类 **Games** 表示一段很复杂的代码，那么这种方式意味着你可以在不同类型的游戏里复用这段代码。你可以再想象一些能够从这个模式中受益的更加精巧的游戏。\n",
    "\n",
    "在下一章，你将会看到一种更加优雅的使用匿名内部类的工厂实现方式。\n",
    "\n",
    "<!-- Summary -->\n",
    "\n",
    "## 本章小结\n",
    "\n",
    "认为接口是好的选择，从而使用接口不用具体类，这具有诱惑性。几乎任何时候，创建类都可以替代为创建一个接口和工厂。\n",
    "\n",
    "很多人都掉进了这个陷阱，只要有可能就创建接口和工厂。这种逻辑看起来像是可能会使用不同的实现，所以总是添加这种抽象性。这变成了一种过早的设计优化。\n",
    "\n",
    "任何抽象性都应该是由真正的需求驱动的。当有必要时才应该使用接口进行重构，而不是到处添加额外的间接层，从而带来额外的复杂性。这种复杂性非常显著，如果你让某人去处理这种复杂性，只是因为你意识到“以防万一”而添加新接口，而没有其他具有说服力的原因——好吧，如果我碰上了这种设计，就会质疑此人所作的所有其他设计了。\n",
    "\n",
    "恰当的原则是优先使用类而不是接口。从类开始，如果使用接口的必要性变得很明确，那么就重构。接口是一个伟大的工具，但它们容易被滥用。\n",
    "\n",
    "<!-- 分页 -->\n",
    "\n",
    "<div style=\"page-break-after: always;\"></div>"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 4
}
